package edu.pw.treegrid.server.reportmodel;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import edu.pw.treegrid.server.classmodel.SimpleAttribute;
import edu.pw.treegrid.server.query.ColumnNode;
import edu.pw.treegrid.server.query.Node;
import edu.pw.treegrid.shared.ReportColumnCategory;
import edu.pw.treegrid.shared.ReportColumnType;

public class CountColumn extends ReportSimpleColumn {
	
	public static final String COUNT_NAME = "liczba";
	
	public CountColumn() {
		super(COUNT_NAME);
		setHierarchyCandidate(false);
		setType(ReportColumnType.INTEGER);
		setTitle("Liczba");
		setWidth("5%");
		setFormat("#,###");
	}

	public ReportColumnCategory getCategory() {
		if(this.getReport().isCountVisible()) {
			return ReportColumnCategory.MEASURE;
		}
		else {
			return ReportColumnCategory.MEASURE_NOT_VISIBLE;
		}
	}
	
	@Override
	public Node buildQueryNode() {
		ColumnNode cn = new CountColumnNode();
		cn.setColumnAlias(COUNT_NAME);
		cn.setReportColumn(this);
		return cn;
	}

	@Override
	public SimpleAttribute getSimpleAttribute() {
		return null;
	}
	
	private static class CountColumnNode extends ColumnNode {
		
		@Override
		public String getBaseExpressionString() {
			if(!isGroupBy()) {
				return "count(*)";
			}
			else {
				return "1";
			}
		}
		
		@Override
		public String getExpressionString() {
			return getBaseExpressionString();
		}

		@Override
		public boolean isMeasure() {
			return true;
		}

	}

	@Override
	protected void serializeToXML(Document document, Element columnElement) {
		//do nothing
	}

	@Override
	protected boolean isSerialize() {
		return false;
	}

}
